/***
 * author: Adam Pociejowski
 */

package requests;

import requests.info.manual.Info;
import requests.info.schedule.Plan;
import requests.math.programmerCalc.ProgrammerCalc;
import requests.music.Mp3Browser;
import requests.music.Mp3Player;
import requests.music.Mp3Random;
import requests.settings.Settings;
import requests.weather.WebsiteReader;
import main.DemiGui;

/** 
 * Important class which controls every user command and delegate it to another classes 
***/

public class Controller implements IRequest {
	
	private DemiGui instance;
	private FileFinder finder; 
	private Mp3Player player;
	private Mp3Browser browser = null;
	private Mp3Random mp3Random = null;
	private Settings settings;
	private String musicPath, mainPath;
	
	public Controller(DemiGui instance, String mainPath, Settings settings) {
		this.mainPath = mainPath;
		this.instance = instance;
		this.settings = settings;
		finder = new FileFinder(instance);
		musicPath = settings.getSettings("musicPath");
		WebsiteReader weather = new WebsiteReader(mainPath+"net.txt", instance, settings);
		weather.start();
	}
	
	/********************************************************************************  
	 *     String command contains for example: "m one" 
	 * 	   m - play mp3, have optional parameters:
	 * 		-l - showing all .mp3 files in path folder
	 *     end - exit program  
	 *     String after space is optional parameter 
	 ********************************************************************************/

	public void task(String command) {
		
		String part[] = command.split(" ");
		
		if (part[0].equalsIgnoreCase("m")) {
			
		/***********  Mp3 Player case in part[1-n] is name of song ***********/
			
			if (part[1].equalsIgnoreCase("end")) {
				if (mp3Random!=null) {
					if (mp3Random.off == 0) {
						mp3Random.stop();
					}
				}
					
				closePlayer();
			}
			else if (part[1].equalsIgnoreCase("-l")) {
				
				if (browser == null) {
					browser = new Mp3Browser(this, instance, finder, musicPath, 330, 600, settings);
				}
				
				browser.showList();
			}
			else if (part[1].equalsIgnoreCase("-r")) {
				
				if (mp3Random == null) {
					mp3Random = new Mp3Random(instance, finder, musicPath, settings);
				}
				
				mp3Random.createPlaylist();
				
			}
			else {
				closePlayer();
				String title = "";
				for (int i = 1; i < part.length - 1; i++) {
					title += part[i] + " ";
				}
				
				/****** finder is instance of class FileFinder which  *
				 *      looking for matches file                *******/
				
				title += part[part.length - 1];
				String path = musicPath;	
				String list[] = finder.Find(musicPath, title);
				if (!list[0].equals("")) {
					path += list[0];
				}
				
				/********************************************************/
				
				/****** If song has been found musinPath != tmp  *******/ 
				
				if (!musicPath.equals(path)) {
					newPlayer(path, list[0]);	
				}
				else {
					returnDEMI("There is no song with this name");
				}
				
				/*******************************************************/
			}
			
		/***********************************************************************/
			
		}
		else if (part[0].equalsIgnoreCase("plan")) {
			 new Plan(1050, 600, settings);
		}
		else if (part[0].equalsIgnoreCase("pcal")) {
			new ProgrammerCalc(520, 390);
			
		}
		else if (part[0].equalsIgnoreCase("info")) {
			new Info(400, 400, instance, mainPath+"info.txt");

		}
		else if (part[0].equalsIgnoreCase("exit")) {

			instance.exit();
		}
		else {
			returnDEMI("Wrong action!");
		}		
	}
	
	/*************************************************************************************/

	
	public void newPlayer(String path, String title) {
		player = new Mp3Player(path, instance, title);	
	}
	
	public void closePlayer() {
		if (player != null) {
			player.singleExit();
		}
	}
	
	@Override
	public void returnDEMI(String text) {
		instance.Monitor(text);
		
	}

}
